package zhishu.iceyyy.com.kd_khd.tcp;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.github.czy1121.view.RoundButton;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import zhishu.iceyyy.com.kd_khd.R;
import zhishu.iceyyy.com.kd_khd.utils.NetUtil;
import zhishu.iceyyy.com.kd_khd.utils.SpfUtils;

/**
 * 现场拍摄记录
 */
public class Locale_Activity extends AppCompatActivity {

    Client client;

    @BindView(R.id.bt_dj)
    RoundButton bt1;

    @BindView(R.id.bt_dj2)
    RoundButton btDj2;

    @BindView(R.id.textView7)
    EditText textView7;
    @BindView(R.id.img_back)
    RelativeLayout imgBack;
    @BindView(R.id.textView11)
    TextView textView11;
    @BindView(R.id.sys)
    RoundButton sys;
    @BindView(R.id.wendu)
    TextView wendu;
    @BindView(R.id.shidu)
    TextView shidu;
    String shuju2,shuju;

    @SuppressLint("HandlerLeak")
    private Handler h = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:

                    break;
                case 2:
                    Log.e("999", "连接失败！");
                    Toast.makeText(Locale_Activity.this, "连接失败，服务器端有问题！！!", Toast.LENGTH_SHORT).show();
                    bt1.setText("开始连接");

                    cons.isConnecting = true;
                    break;
                case 3:
                    Toast.makeText(Locale_Activity.this, "断开连接！", Toast.LENGTH_SHORT).show();
                    Log.e("888", "断开连接！");
                    bt1.setText("开始连接");
                    cons.isConnecting = true;

                    break;
                case 4:
                    Toast.makeText(Locale_Activity.this, "服务器连接成功！", Toast.LENGTH_SHORT).show();
                    Log.e("888", "服务器连接成功！");
                    cons.isConnecting = false;
                    bt1.setText("断开连接");


                    break;
                case 5:
                    String ss = (String) msg.obj;
                    Toast.makeText(Locale_Activity.this, ""+ss, Toast.LENGTH_SHORT).show();
                    Log.e("888", ss);
                    try {
                        if (ss.contains("t:")&&ss.contains("c:")) {
                            shuju = ss.substring(ss.indexOf("t:") + 2, ss.indexOf("c:"));
                        }

                        if (ss.contains("h:")&&ss.contains("m")) {
                              shuju2 = ss.substring(ss.indexOf("h:") + 2, ss.indexOf("m"));
                        }
                        wendu.setText("温度："+shuju);
                        shidu.setText("湿度："+shuju2);
                        if (ss.contains("w")) {
                            //播放声音
                            if (!mediaPlayer.isPlaying()) {
                                mediaPlayer.start();
                            }



                        } else {

                        }

                    } catch (Exception e) {
                        Toast.makeText(Locale_Activity.this, "异常" + e.getMessage(), Toast.LENGTH_SHORT).show();


                    }


                    //未录制


                    break;
                default:
                    break;
            }

        }
    };


    List<String> strings;
    StringBuilder ST;

    MediaPlayer mediaPlayer;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_locale);
        strings = new ArrayList<>();
        ButterKnife.bind(this);
        ST = new StringBuilder();

         mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.a);
//        try {
////            mediaPlayer.prepare();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }



    }


    // 取消注册
    @Override
    public void onDestroy() {
        super.onDestroy();
        cons.isConnecting = true;
        if (client != null) {
            client.gb();
        }
        mediaPlayer.stop();
    }

    @OnClick({R.id.sys, R.id.bt_dj2, R.id.bt_dj, R.id.img_back, R.id.bt_stop})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.bt_dj2:
                if (cons.isConnecting) {
                    Toast.makeText(Locale_Activity.this, "请检查网络!", Toast.LENGTH_SHORT).show();
                    bt1.setText("开始连接");
                } else {
                    //发送数据
                    String s = textView7.getText().toString();
                    if (TextUtils.isEmpty(s)) {
                        Toast.makeText(this, "请输入数据！", Toast.LENGTH_SHORT).show();
                        break;

                    }

                    kaishifasong(s);
                }


                break;
            case R.id.img_back:

               finish();
//                if (!mediaPlayer.isPlaying()) {
//                    mediaPlayer.start();
//                }
                break;
            case R.id.bt_stop:

                if (mediaPlayer.isPlaying()) {
                    mediaPlayer.stop();
                    try {
                        mediaPlayer.prepare();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }


                break;

            case R.id.sys:

                Intent intent = new Intent(this, SZIP_Activity.class);
                startActivityForResult(intent, 999);

                break;

            case R.id.bt_dj:
                if (cons.isConnecting) {
                    final String duankou = (String) SpfUtils.get(Locale_Activity.this, "duankou", "8080");

                    if (NetUtil.isNetworkAvailable(Locale_Activity.this)) {

                        new AsyncTask<Void, Void, Void>() {
                            @Override
                            protected Void doInBackground(Void... params) {
                                client = new Client((String) SpfUtils.get(Locale_Activity.this, "ip", "192.168.43.1"), Integer.parseInt(duankou), h);
                                return null;
                            }
                        }.execute();

                    } else {
                        Toast.makeText(Locale_Activity.this, "请检查网络!", Toast.LENGTH_SHORT).show();
                        bt1.setText("开始连接");
                    }


                } else {
                    bt1.setText("开始连接");
                    client.gb();
                    cons.isConnecting = true;

                }
                break;
        }
    }


    private void kaishifasong(String s) {
        client.fasong(s);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == 999) {
            Bundle b = data.getExtras(); //data为B中回传的Intent
            String str = b.getString("ewm");//str即为回传的值
            if (!TextUtils.isEmpty(str)) {
                strings.add(str);

            }

        }
    }
}
